<?php
class System_SettingsController extends Promotor_Controller_Action {

    public $skin = array(
		// forsuje domyslny template i style na admin
		'template' => 'admin',
		'style' => 'default',
		// layout
		'layout' => 'admin_system',
		// rozszerzam konfiguracje
		'config' => array(
			'filename' => 'backend_config.ini'
		),
    );
    
    public $acl = array(
		'index' => 'update',
		'module' => 'update',
		'application' => 'update'
	);

    public function indexAction() {
    	$this->_forward('application');
    }
    
	/**
	 * Konfiguruje moduly
	 * @return void
	 */
	public function moduleAction() {
        $rq = $this->getRequest();
        $model = new System_Model_Config();

        $paths = $model->getModulesPathnames();
        $this->view->rowset = $paths;
        
		if (null=== ($config = $rq->getQuery('config'))) {
        	return;
        }

        $model->setSkipExtends($rq->getQuery('skipExtends', 1));
        $model->setModuleFilenameFromKey($config);

        $form = $model->getForm();

        $status = $model->getStatus();
        if ($status === System_Model_Config::FAILURE) {
        	$flashMessenger = $this->_helper->getHelper('flashMessenger');
        	$flashMessenger->addMessage($status);
        	$messages = $model->getMessages();
			foreach ($messages as $message) {
				$flashMessenger->addMessage($message);
			}
        	return;
        }

        if (!$rq->isPost()) {
            $this->view->form = $form;
            return;
        }

        if (!$form->isValid($rq->getPost())) {
            $this->view->form = $form;
            return;
        }

        $model->save($form->getValues(true));
        
    	$status = $model->getStatus();
    	
    	$flashMessenger = $this->_helper->getHelper('flashMessenger');
    	$flashMessenger->addMessage($status);

		if ($status == System_Model_Config::SUCCESS) {
			$this->_helper->redirector->goToUrlAndExit(getenv('HTTP_REFERER'));
		} else {
			$messages = $model->getMessages();
			foreach ($messages as $message) {
				$flashMessenger->addMessage($message);
			}

			$this->view->form = $form;
		}
    }
    
    /**
	 * Konfiguruje applikacje
	 * @return void
	 */
	public function applicationAction() {
        $rq = $this->getRequest();
        $model = new System_Model_Config();

        $paths = array_keys($model->getApplicationPathnames());
        $this->view->rowset = array_combine($paths, $paths);
        
		if (null=== ($config = $rq->getQuery('config'))) {
        	return;
        }

        $model->setSkipExtends($rq->getQuery('skipExtends', 1));
        $model->setFilename(@$this->view->rowset[$config]);

        $form = $model->getForm();

        $status = $model->getStatus();
        if ($status === System_Model_Config::FAILURE) {
        	$flashMessenger = $this->_helper->getHelper('flashMessenger');
        	$flashMessenger->addMessage($status);
        	$messages = $model->getMessages();
			foreach ($messages as $message) {
				$flashMessenger->addMessage($message);
			}
        	return;
        }

        if (!$rq->isPost()) {
            $this->view->form = $form;
            return;
        }

        if (!$form->isValid($rq->getPost())) {
            $this->view->form = $form;
            return;
        }

        $model->save($form->getValues(true));
        
    	$status = $model->getStatus();
    	
    	$flashMessenger = $this->_helper->getHelper('flashMessenger');
    	$flashMessenger->addMessage($status);

		if ($status == System_Model_Config::SUCCESS) {
			$this->_helper->redirector->goToUrlAndExit(getenv('HTTP_REFERER'));
		} else {
			$messages = $model->getMessages();
			foreach ($messages as $message) {
				$flashMessenger->addMessage($message);
			}

			$this->view->form = $form;
		}
    }
}